package cn.pconline.officialclub.web.wap;

import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.util.DateUtils;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.autoclub.web.UserAuth;
import cn.pconline.autoclub.web.wap.ActionKit;
import cn.pconline.officialclub.service.OClubService;
import cn.pconline.officialclub.service.OStatService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author xulin
 */
@Controller
@RequestMapping(value="/wap/brand/stat")
public class OStatAction {
    @Autowired
    private OStatService oStatService;
    @Autowired
    private OClubService oClubService;
    @Autowired
    private UserAuth userAuth;
    /**
     * WAP首页增加访问量
     * @param req
     * @param resp 
     */
    @ResponseBody
    @RequestMapping(value = "/addPv.do", method = RequestMethod.POST)
    public void addPv(HttpServletRequest req, HttpServletResponse resp) {
        WebUtils.setEncoding(req, resp);
        long oClubId = WebUtils.paramLong(req, "oClubId", 0);
        if (oClubId > 0) {
            oStatService.createOrUpdatePv(oClubId, DateUtils.getTodayYmd());
            ActionKit.printSuccessMsg(resp, "操作成功", "");
        } else {
            ActionKit.printErrorMsg(resp, "操作失败","");
        }
    }
    /**
     * WAP首页增加访问记录
     * @param req
     * @param resp 
     */
    @RequestMapping(value = "/addVisitor.do", method = RequestMethod.POST)
    @ResponseBody
    public void addVisitor(HttpServletRequest req, HttpServletResponse resp) {
        WebUtils.setEncoding(req, resp);
        long oClubId = WebUtils.paramLong(req, "oClubId", 0);
        if (oClubId > 0 && oClubService.findValidClub(oClubId) != null) {
            User user = userAuth.getUser(req, resp);
            if (user == null || user.getUid()==0) {
                return;
            }
            int count = oStatService.addOrUpdateVisitor(user.getUid(), oClubId);
            if (count > 0) {
                ActionKit.printSuccessMsg(resp, "操作成功", "");
            } else {
                ActionKit.printErrorMsg(resp, "操作失败","");
            }
        }
    }
}
